package controller.business.observerReport;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import itext.ItextFont;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

public class ObserverReportPdf {
    public   static  void createPdf(OutputStream outputStream, Map<String, String> map) throws DocumentException, IOException {
        //设置纸张大小
        Document document = new Document(PageSize.A4, 90.14F, 90.14F, 72F, 72F);

        //建立一个书写器，与document对象关联
        PdfWriter.getInstance(document, outputStream);
        document.open();


        Paragraph paragraph = new Paragraph(map.get("title"), ItextFont.fangZhengPdf18);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);

        paragraph = new Paragraph("区县市场监管局（盖章）：", ItextFont.fangSongNormalPdf12);
        paragraph.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph);

        paragraph = new Paragraph("观察员（签字）：", ItextFont.fangSongNormalPdf12);
        paragraph.setAlignment(Element.ALIGN_LEFT);
        paragraph.setSpacingBefore(15);
        document.add(paragraph);


        //设置Table表格,创建一个7列的表格
        PdfPTable table = new PdfPTable(5);
        float width[] = {1.2F, 1.3F, 1, 1, 1};//设置每列宽度比例
        table.setWidths(width);
        table.setWidthPercentage(100);//占页面宽度比例
        PdfPCell[] cell = new PdfPCell[24];


        cell[0] = new PdfPCell(new Phrase("核查计划编号", ItextFont.fangSongNormalPdf12));
        cell[1] = new PdfPCell(new Phrase(map.get("businessCode"), ItextFont.fangSongNormalPdf12));
        cell[1].setColspan(4);

        cell[2] = new PdfPCell(new Phrase("企业名称", ItextFont.fangSongNormalPdf12));
        cell[3] = new PdfPCell(new Phrase(map.get("companyName"), ItextFont.fangSongNormalPdf12));
        cell[3].setColspan(2);

        cell[4] = new PdfPCell(new Phrase("申请类别", ItextFont.fangSongNormalPdf12));
        cell[5] = new PdfPCell(new Phrase(map.get("businessType"), ItextFont.fangSongNormalPdf12));

        cell[6] = new PdfPCell(new Phrase("产品名称", ItextFont.fangSongNormalPdf12));
        cell[7] = new PdfPCell(new Phrase(map.get("productName"), ItextFont.fangSongNormalPdf12));
        cell[7].setColspan(2);
        cell[8] = new PdfPCell(new Phrase("核查结果", ItextFont.fangSongNormalPdf12));
        cell[9] = new PdfPCell(new Phrase(map.get("result"), ItextFont.fangSongNormalPdf12));

        cell[10] = new PdfPCell(new Phrase("通知日期", ItextFont.fangSongNormalPdf12));
        cell[11] = new PdfPCell(new Phrase( map.get("noticeTime"), ItextFont.fangSongNormalPdf12));
        cell[12] = new PdfPCell(new Phrase("核查日期", ItextFont.fangSongNormalPdf12));
        cell[13] = new PdfPCell(new Phrase( map.get("checkTime"), ItextFont.fangSongNormalPdf12));
        cell[13].setColspan(2);

        cell[14] = new PdfPCell(new Phrase("审查组长", ItextFont.fangSongNormalPdf12));
        cell[15] = new PdfPCell(new Phrase(map.get("leader"), ItextFont.fangSongNormalPdf12));
        cell[16] = new PdfPCell(new Phrase("审查组成员", ItextFont.fangSongNormalPdf12));
        cell[17] = new PdfPCell(new Phrase(map.get("crew"), ItextFont.fangSongNormalPdf12));
        cell[17].setColspan(2);

        cell[18] = new PdfPCell(new Phrase("核查情况\n简述\n", ItextFont.fangSongNormalPdf12));
        cell[19] = new PdfPCell(new Phrase(map.get("checkStatus"), ItextFont.fangSongNormalPdf12));
        cell[19].setColspan(4);

        cell[20] = new PdfPCell(new Phrase("核查组纪律\n执行情况\n", ItextFont.fangSongNormalPdf12));
        cell[21] = new PdfPCell(new Phrase(map.get("checkDiscipline"), ItextFont.fangSongNormalPdf12));
        cell[21].setColspan(4);

        cell[22] = new PdfPCell(new Phrase("备注\n", ItextFont.fangSongNormalPdf12));
        cell[23] = new PdfPCell(new Phrase(map.get("remark"), ItextFont.fangSongNormalPdf12));
        cell[23].setColspan(4);


        for (int i = 0; i < 24; i++) {
            if (i == 19 || i == 21 || i == 23) {
                cell[i].setHorizontalAlignment(Element.ALIGN_LEFT);
                cell[i].setVerticalAlignment(Element.ALIGN_TOP);
                cell[i].setMinimumHeight(70);
            } else {
                cell[i].setHorizontalAlignment(Element.ALIGN_CENTER);//居中显示
                cell[i].setVerticalAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
            }
            cell[i].setPaddingTop(10.5F);
            cell[i].setPaddingBottom(10.5F);
            table.addCell(cell[i]);
        }


        paragraph = new Paragraph();
        paragraph.add(table);
        paragraph.setSpacingBefore(5f);
        document.add(paragraph);

        paragraph = new Paragraph("注：1、此表由工业产品生产许可证观察员填写，相关信息要准确、真实。\n" +
                "　　2、此表一个企业填写一份，一式两份。其中，一份填好后应于核查结束后三日内寄送上海市质量技术监督局业务受理中心；" +
                "一份由区县市场监管局留。", ItextFont.fangSongNormalPdf12);
        document.add(paragraph);
        document.close();

    }

    public static void main(String[] args) {

        String file = "d:/test11.pdf";
        Map map = new HashMap();
        map.put("title", "上海市工业产品生产许可证现场核查观察工作报告");
        map.put("observerName", "observerName");
        map.put("businessCode", "businessCode");
        map.put("companyName", "companyName");
        map.put("businessType", "businessType");
        map.put("productName", "productName");
        map.put("result", "result");
        map.put("noticeTime", "2017");
        map.put("checkTime", "2018");
        map.put("leader", "leader");
        map.put("crew", "crew");
        map.put("checkStatus", "checkStatus");
        map.put("checkDiscipline", "checkDiscipline");
        map.put("remark", "remark");


        try {
            OutputStream outputStream=new FileOutputStream(file);
            createPdf(outputStream, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


